//
//  NewFeatureController.swift
//  新浪微博项目
//
//  Created by 王炯 on 16/7/23.
//  Copyright © 2016年 王炯. All rights reserved.
//

import UIKit

class NewFeatureController: UIViewController
{

    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        //重新布局界面
        setUpUI()
    }
    
    //重新布局界面,设置collectionView
    func setUpUI()
    {
        view.addSubview(self.newFeatureCollection)
        
        //设置约束
        self.newFeatureCollection.snp_updateConstraints { (make) in
            
            make.top.left.bottom.right.equalTo(view).offset(0)
        }
    }
    
    

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
    }
    
    
    //懒加载一个collectionView
    private lazy var newFeatureCollection:UICollectionView = {
        
        //flowLayout
        let flowLayout = UICollectionViewFlowLayout()
        
        // 设置itemsize
        flowLayout.itemSize = CGSize(width: SCREENW, height: SCREENH)
        // 设置垂直和水平间距
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.minimumLineSpacing = 0
        // 设置滚动方向
        flowLayout.scrollDirection = .Horizontal
        
        
        
        let collection = UICollectionView(frame:CGRectZero, collectionViewLayout: flowLayout)
        
        //注册cell
        collection.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: "newCell")
        
        
        collection.backgroundColor = RandomColor()
        collection.dataSource = self
        collection.delegate = self
        // 设置分页
        collection.pagingEnabled = true
        // 取消弹簧效果
        collection.bounces = false
        // 隐藏滚动条
        collection.showsHorizontalScrollIndicator = false
        collection.showsVerticalScrollIndicator = false
        
        
        return collection
        
        
        }()
    
}

//实现collectionView的数据源方法和代理方法
extension NewFeatureController:UICollectionViewDataSource,UICollectionViewDelegate
{
    //(1)组
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int
    {
        return 1
    }
    
    //(2)item
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        return 4
    }
    //(3)item内容
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
    {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("newCell", forIndexPath: indexPath) as! NewFeatureCell
        
        //cell.backgroundColor = RandomColor()
        
        //设置图片
        cell.imgStr = "new_feature_\(indexPath.item + 1)"
        
        return cell
        
    }
    //(4)collectionView滑动到第四张时,button显示出来
    func scrollViewDidEndDecelerating(scrollView: UIScrollView)
    {
        let index = Int(scrollView.contentOffset.x / SCREENW)
        
        //当滑动到最后一个时
        if index == 3
        {
            let cell = self.newFeatureCollection.cellForItemAtIndexPath(NSIndexPath(forItem: index, inSection: 0)) as! NewFeatureCell
            // 开始动画
            cell.startAnim()
        }
        
        
    }
}
